/*
 * Copyright (C) 2019 ByteDance Inc
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.bytedance.scene.dialog;


import com.bytedance.scene.dialog.bottomsheet.Constants;
import com.bytedance.scene.dialog.bottomsheet.Gravity;
import com.bytedance.scene.dialog.bottomsheet.SlidingUpBuilder;
import com.bytedance.scene.dialog.bottomsheet.SlidingUpPanel;
import com.bytedance.scene.navigation.NavigationScene;
import com.bytedance.scene.navigation.NavigationSceneGetter;
import ohos.aafwk.content.Intent;
import ohos.agp.components.Component;
import ohos.agp.components.ComponentContainer;
import ohos.agp.components.DirectionalLayout;
import ohos.agp.components.LayoutScatter;

/**
 * Created by JiangQi on 8/9/18.
 */
public abstract class   BottomSheetDialogScene extends DialogScene{
    public BottomSheetDialogScene() {
    }

    @Override
    public Component onCreateView(LayoutScatter scatter, ComponentContainer container, Intent intent) {
        Component contentView = onCreateContentView(scatter, container, intent);
        return wrapInBottomSheet(0, contentView, null);
    }

    protected abstract Component onCreateContentView(LayoutScatter inflater, ComponentContainer container, Intent savedInstanceState);

    private Component wrapInBottomSheet(int i, Component contentView, Object o) {
        Component component = LayoutScatter.getInstance(getActivity()).parse(ResourceTable.Layout_bottomsheetdialog,null,false);
        DirectionalLayout slideView = (DirectionalLayout) component.findComponentById(ResourceTable.Id_slideView);
        DirectionalLayout container2 = (DirectionalLayout) component.findComponentById(ResourceTable.Id_container2);
        DirectionalLayout rootView = (DirectionalLayout) component.findComponentById(ResourceTable.Id_rootView);
//        DirectionalLayout headView = (DirectionalLayout) component.findComponentById(ResourceTable.Id_mainLayout);
        DirectionalLayout.LayoutConfig layoutConfig = new DirectionalLayout.LayoutConfig();
        layoutConfig.width = DirectionalLayout.LayoutConfig.MATCH_PARENT;
        layoutConfig.height = DirectionalLayout.LayoutConfig.MATCH_PARENT;
        Component dim = component.findComponentById(ResourceTable.Id_dim);
        container2.addComponent(contentView,layoutConfig);

        SlidingUpPanel slidingUpPanel  = new SlidingUpBuilder(slideView)
                .withListeners(new SlidingUpPanel.Listener.Events() {
                    @Override
                    public void onSlide(float percent) {
                        dim.setAlpha(1 - (percent / Constants.HUNDERED_NUM_CONST));
                    }

                    @Override
                    public void onVisibilityChanged(int visibility) {
                    }
                })
                .withStartGravity(Gravity.BOTTOM)
                .withLoggingEnabled(true)
                .withGesturesEnabled(true)
                .withStartState(SlidingUpPanel.State.HIDDEN)
                .withSlideFromOtherView(component.findComponentById(ResourceTable.Id_rootView))
                .build();
        DirectionalLayout directionalLayout = (DirectionalLayout) component.findComponentById(ResourceTable.Id_rootView);
        directionalLayout.setClickedListener(component1 -> {
            cancel();
        });
        return component;
    }
    private void cancel() {
        NavigationScene navigationScene = NavigationSceneGetter.getNavigationScene(this);
        if (navigationScene != null) {
            navigationScene.remove(this);
        }
    }
}
